package com.zhanglinwei.openApiServer.listener;

import com.zhanglinwei.common.SpringContext;
import com.zhanglinwei.openApiServer.collect.OpenApiCollect;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class ApplicationReadyEventListener {

    @EventListener({ApplicationReadyEvent.class})
    public void applicationReady() {
        doCollectOpenApi();
    }

    private void doCollectOpenApi() {
        Map<String, OpenApiCollect> openApiCollectMap = SpringContext.getBeansOfType(OpenApiCollect.class);
        if (MapUtils.isEmpty(openApiCollectMap)) {
            log.info("## ApplicationReadyEventListener.doCollectOpenApi --> OpenApiCollect not found");
            return;
        }

        for (OpenApiCollect openApiCollect : openApiCollectMap.values()) {
            openApiCollect.collect();
        }
    }

}
